﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace PrisonDefence.Models
{
    class Level
    {
        Map map;
        VertexPositionColorTexture[] verts;
        int[] indices;
        BasicEffect effect;



        public Level(Map map, Texture2D hieghtMap, BasicEffect effect)
        {
            this.map = map;
            this.effect = effect;

            Initialize(hieghtMap);
        }

        private void Initialize(Texture2D heightMap)
        {
            List<VertexPositionColorTexture> v = new List<VertexPositionColorTexture>();
            List<int> i = new List<int>();

            for (int x = 0; x < map.Width; x++)
            {
                for (int y = 0; y < map.Height; y++)
                {
                    int textureIndex = map.GetIndex(x, y);
                    //if (textureIndex == -1)
                    //    continue;

                    Color color = Color.DarkGreen;
                    if (textureIndex == 1)
                        color = Color.Green;

                    i.Add(v.Count);
                    i.Add(v.Count + 1);
                    i.Add(v.Count + 2);
                    i.Add(v.Count);
                    i.Add(v.Count + 3);
                    i.Add(v.Count + 1);

                    v.Add(new VertexPositionColorTexture(new Vector3(0.0f + x, 0, 0.0f + y), color, new Vector2(0, 0)));
                    v.Add(new VertexPositionColorTexture(new Vector3(1.0f + x, 0, 1.0f + y), color, new Vector2(0, 0)));
                    v.Add(new VertexPositionColorTexture(new Vector3(0.0f + x, 0, 1.0f + y), color, new Vector2(0, 0)));

                    //v.Add(new VertexPositionColorTexture(new Vector3(0.0f + x, 0, 0.0f + y), color, new Vector2(0, 0)));
                    v.Add(new VertexPositionColorTexture(new Vector3(1.0f + x, 0, 0.0f + y), color, new Vector2(0, 0)));
                    //v.Add(new VertexPositionColorTexture(new Vector3(1.0f + x, 0, 1.0f + y), color, new Vector2(0, 0)));
                }
            }

            verts = v.ToArray();
            indices = i.ToArray();
        }

        public void Update(GameTime gameTime)
        {
        }

        public void Draw(GraphicsDevice graphicsDevice, GameTime gameTime, Matrix viewMatrix, Matrix projectionMatrix)
        {
            effect.View = viewMatrix;
            effect.Projection = projectionMatrix;
            effect.VertexColorEnabled = true;

            foreach (var pass in effect.CurrentTechnique.Passes)
            {
                pass.Apply();
                graphicsDevice.DrawUserIndexedPrimitives<VertexPositionColorTexture>(PrimitiveType.TriangleList,
                     verts, 0, verts.Length, indices, 0, indices.Length / 3);

                //GraphicsDevice.DrawUserPrimitives<VertexPositionColorTexture>(PrimitiveType.TriangleList, verts, 0, verts.Length / 3);
            }
        }
    }
}
